﻿Imports System.Data.SqlClient
Module ModuleTenDangNhap
    Public m_tennhap As String
    Public m_DangNhapThanhCong As Boolean = False
    Public phanquyen As String
    Public tenphanhe As String
    Public _sohoso As String
End Module
Public Class FrmDangNhap
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select * from dbo.login where TenDangNhap= '" & TextTenDangNhap.Text & "'  and MatKhau= '" & TextMatKhau.Text & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        ob_cnn.Close()

        If tbl.Rows.Count > 0 Then
            Dim Sql As String = ""
            Sql = " insert into QLDangNhap (NguoiDangNhap, NgayDangNhap) values (@nguoidangnhap, @ngaydangnhap)"
            Dim cmd As New SqlCommand(Sql, ob_cnn)
            cmd.Parameters.AddWithValue("@nguoidangnhap", TextTenDangNhap.Text)
            cmd.Parameters.AddWithValue("@ngaydangnhap", DateTime.Now)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            m_DangNhapThanhCong = True
            m_tennhap = TextTenDangNhap.Text

            'Dim dr As DataRow
            'dr = tbl.Rows(0)
            'phanquyen = dr("phanquyenid").ToString()
            'ob_cnn.Close()


            NapQuyen()
            MessageBox.Show("Đăng nhập thành công")
            ob_cnn.Close()
            Me.Close()
        Else
            MessageBox.Show("Sai thông tin")
        End If

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub FrmDangNhap_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub NapQuyen()
        Dim tbl As DataTable
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select * " &
                                " From login  " &
                                " WHERE  " &
                                " tendangnhap = '" & TextTenDangNhap.Text & "'", ob_cnn)
        tbl = New DataTable()
        tbl.Load(dbComm.ExecuteReader())
        Dim dr As DataRow
        dr = tbl.Rows(0)
        phanquyen = dr("QuyenHanID").ToString().Trim()
        tenphanhe = dr("PhanHeCapQuyen").ToString().Trim()
        ob_cnn.Close()
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub TextMatKhau_TextChanged(sender As Object, e As EventArgs) Handles TextMatKhau.TextChanged

    End Sub

    Private Sub TextTenDangNhap_TextChanged(sender As Object, e As EventArgs) Handles TextTenDangNhap.TextChanged

    End Sub
End Class
